<template>
  <div class="app">
    <el-form :inline="true" :model="queryForm" @keyup.enter.native="m_onSubmit">
      <el-form-item label="用户名">
        <el-input v-model="queryForm.query" placeholder="用户名" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="m_onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-row class="p-b">
      <el-col>
        <el-button v-permission="['users_131_03']" @click="addUser">添加</el-button>
        <el-button @click="m_exportData">导出</el-button>
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
    >
      <el-table-column
        prop="username"
        label="用户名"
      />
      <el-table-column
        prop="role_name"
        label="角色"
      />
      <el-table-column
        label="状态"
      >
        <template slot-scope="scope">
          {{ scope.row.mg_state ? '启用': '禁用' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="mobile"
        label="电话"
      />
      <el-table-column
        prop="email"
        label="邮箱"
      />
      <el-table-column
        label="注册时间"
      >
        <template slot-scope="scope">
          {{ scope.row.create_time | parseTime }}
        </template>
      </el-table-column>
      <el-table-column
        width="80"
        label="操作"
      >
        <template slot-scope="scope">
          <el-dropdown v-if="scope.row.id !== 500">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item @click.native="editUser(scope.row.id)">编辑</el-dropdown-item>
              <el-dropdown-item @click.native="putUserState(scope.row)"> <span>{{ scope.row.mg_state ?'禁用': '启用' }}</span></el-dropdown-item>
              <el-dropdown-item @click.native="editUserRole(scope.row)">角色</el-dropdown-item>
              <el-dropdown-item @click.native="deleteUser(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="m_pagination.total"
      :page.sync="m_pagination.page"
      :limit.sync="m_pagination.pageSize"
      @pagination="loadTableData"
    />
    <user-form :id="userId" :visible.sync="dialogVisible" :type="type" :cb="loadTableData" />
    <role-form :visible.sync="roleVisible" :user="user" :cb="loadTableData" />
  </div>
</template>

<script>
import { getUsers, deleteUser, putUserState } from '@/api/user'
import importAll from '@/utils/importAll'
import { excel, pagination, query } from '@/mixins'
import { parseTime } from '@/utils'
const components = importAll(require.context('./components', true, /\.vue$/))

// 导出 excel
const tHeader = ['用户名', '角色', '状态', '电话', '邮箱', '注册时间']
const filterVal = ['username', 'role_name', 'mg_state', 'mobile', 'email', 'create_time']
const excelName = '用户列表'

export default {
  name: 'USERSLIST',
  components: {
    ...components
  },
  mixins: [pagination(), query(), excel(tHeader, filterVal, excelName, 'excelData')],
  data() {
    return {
      queryForm: {
        query: ''
      },
      tableData: [],
      dialogVisible: false,
      type: 1,
      userId: '',
      roleVisible: false,
      user: {},
      excelData: []
    }
  },
  created() {
    this.loadTableData()
  },
  methods: {
    async loadTableData() {
      const params = {
        query: this.queryForm.query,
        pagenum: this.m_pagination.page,
        pagesize: this.m_pagination.pageSize
      }
      const { users, total } = await getUsers(params)
      this.tableData = users
      this.m_pagination.total = total
      this.getExcelData()
    },
    addUser() {
      this.type = 1
      this.dialogVisible = true
    },
    async deleteUser(user) {
      this.$confirm(`删除${user.username}, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteUser(user.id)
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.loadTableData()
      }).catch(() => {
        console.log('取消删除')
      })
    },
    editUser(id) {
      this.type = 2
      this.userId = id
      this.dialogVisible = true
    },
    async putUserState(user) {
      console.log('user:', user)
      await putUserState(user)
      this.$message({
        message: '更改成功',
        type: 'success'
      })
      this.loadTableData()
    },
    editUserRole(user) {
      this.user = user
      this.roleVisible = true
    },
    getExcelData() {
      this.excelData = this.tableData.map(i => {
        i.mg_state = i.mg_state ? '启用' : '禁用'
        i.create_time = parseTime(i.create_time)
        return i
      })
    }
  }
}
</script>

<style lang="less" scoped>

</style>
